package com.tianhui.consignortwofei.mvp.ui.activity.editPicture;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.imageView.PictureDialog;
import com.fgs.common.imageView.strategy.ImageLoader;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseFragment;
import com.tianhui.consignortwofei.constant.KeyValue;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.model.OrderModel;
import com.tianhui.consignortwofei.mvp.model.enty.OrderInfo;
import com.tianhui.consignortwofei.mvp.ui.activity.OrderUploadPictureActivity;
import com.xuexiang.xui.widget.dialog.materialdialog.DialogAction;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/4/2
 * @description 修改图片
 **/
public abstract class BaseEditPictureActivity extends OrderUploadPictureActivity {

    @BindView(R.id.activity_base_edit_order_picture_titleTextView)
    TextView mTitleTextView;
    @BindView(R.id.activity_base_edit_order_picture_vehicleImageRecyclerView)
    RecyclerView mVehicleImageRecyclerView;
    @BindView(R.id.activity_base_edit_order_picture_imageView)
    ImageView mPictureImageView;

    private OrderModel mOrderModel;
    private PictureDialog pictureDialog;

    @Override
    public int getLayoutId() {
        return R.layout.activity_base_edit_order_picture;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public void init() {

        super.init();
        setTitle(getImageTitle());
        mOrderModel = new OrderModel();

        Bundle bundle = getIntent().getExtras();
        if (bundle != null && !bundle.isEmpty()) {
            String dispatchNo = bundle.getString(KeyValue.KEY_DISPATCH_NO);
            requestOrderDetailInfo(dispatchNo);
        }

        mTitleTextView.setText(getImageTitle());

        initListener();
    }

    private void requestOrderDetailInfo(String dispatchNo) {
        if (!TextUtils.isEmpty(dispatchNo)) {
            Map<String, String> map = new HashMap<>();
            map.put("dispatchno", dispatchNo);
            mOrderModel.orderSourceInfoModel(this, map, true, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<OrderInfo>>() {
                @Override
                public void onNext(CommonResponse<OrderInfo> commonResponse) {
                    if (commonResponse.getCode() == 20000) {
                        mOrderInfo = commonResponse.getData();
                        mOrderId = mOrderInfo.dispatchno;
                        setPictureImage(mOrderInfo);

                    } else {
                        showToast(commonResponse.getMsg());
                    }

                }

                @Override
                public void onError(ExceptionHandle.ResponseThrowable e) {
                    showToast(e.message);
                }
            });
        }
    }


    private void setPictureImage(OrderInfo orderInfo) {
        if (orderInfo != null) {
            List<String> list = getImageId(orderInfo);
            if (list != null && list.size() > 0)
                ImageLoader.init().loadImageWithId(mPictureImageView, list.get(0));
        }
    }

    public abstract String getImageType();

    public abstract Map<String, String> requestMap(Map<String, String> map);

    public abstract String getImageTitle();

    public abstract List<String> getImageId(OrderInfo orderInfo);


    private void initListener() {
        mPictureImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<String> list = getImageId(mOrderInfo);
                if (list != null && list.size() > 0) {
                    pictureDialog = new PictureDialog(BaseEditPictureActivity.this);
//                    pictureDialog.setCanSavePicture(true);
                    pictureDialog.show();
                    pictureDialog.setImageUrlList(list);
                } else {
                    showToast("暂无相关图片");
                }
            }
        });
    }


    @Override
    public RecyclerView getVehicleImageRecyclerView() {
        return mVehicleImageRecyclerView;
    }

    @Override
    public RecyclerView getInvoicesImageRecyclerView() {
        return null;
    }


    @Override
    public BaseFragment createFragment() {
        return null;
    }


    @OnClick(R.id.activity_base_edit_order_picture_commitButton)
    public void commit() {

        if (TextUtils.isEmpty(mOrderId)) {
            showToast("未获取到订单号");
            return;
        }

        String imageId = getVehicleImageId();
        if (TextUtils.isEmpty(imageId)) {
            showToast("请先上传图片");
            return;
        }

        Map<String, String> map = new HashMap<>();
        map.put("dispatchno", mOrderId);
        map = requestMap(map);

        requestConfirmLoading(map);
    }

    private void requestConfirmLoading(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            showToast("获取信息失败，请重新进入当前界面");
            return;
        }
        //updateOrderPictureModel
        //confirmLoadingChangePictureModel
        mOrderModel.updateOrderPictureModel(this, map, true, this.bindToLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    EventBusManage.refreshOrderDetail();
                    showConfirmDialog();
                } else {
                    showToast(commonResponse.getMsg());
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
            }
        });
    }

    private void showConfirmDialog() {
        new MaterialDialog.Builder(this)
                .title("提示")
                .content("修改成功")
                .positiveText("查看")
                .onPositive(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        requestOrderDetailInfo(mOrderId);
                    }
                })
                .negativeText("返回")
                .onNegative(new MaterialDialog.SingleButtonCallback() {
                    @Override
                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                        finish();
                    }
                })
                .canceledOnTouchOutside(false)
                .cancelable(false)
                .show();
    }


    public List<String> stringToList(String string) {
        List<String> list = new ArrayList<>();
        if (!TextUtils.isEmpty(string)) {
            if (string.contains(",")) {
                String[] strings = string.split(",");
                if (strings != null && strings.length > 0) {
                    int length = strings.length;
                    for (int i = 0; i < length; i++) {
                        list.add(strings[i]);
                    }
                }
            } else {
                list.add(string);
            }
        }
        return list;
    }
}
